<template>
	<om-addpage :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="false">
		<om-autoform ref="autoform" :initConfigData="initConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				initValue:{
					salaryPosId:[],
					overTimeTypeId:[],
					overTimeTypeName:[],
					overTimeTypeRatio:[]
				},
				userCacheKey: "tempInstTemplate-user-cache",
				defaultDataCache: "tempInstTemplate-default-user-cache",
				posListCache:'tempInstTemplate-default-posList-cache',
				tempInstTemplateCache:'tempInstTemplate-data-cache',
				prevPage: "",
				instId:"",
				initConfigData: [{
					key: "title0",
					editors: [{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "userName",
							prompt: "请选择",
							required: true,
							title: "姓名",
							type: "linkbox",
							clearFunc:'clearUser',
							valueField: "userId",
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							link: "",
							linkParam: {},
							max: "",
							min: "",
							nameField: "reasonName",
							prompt: "请选择",
							required: false,
							title: "事由",
							data:[],
							type: "selectbox",
							valueField: "reasonCode",
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							data: [],
							editReadonly: true,
							nameField: "posName",
							required: false,
							title: "拟任职岗位",
							prompt: "请选择",
							type: "linkbox",
							valueField: "posId"
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							data:[],
							max: "",
							min: "",
							nameField: "planWorkWayName",
							onSelect: "",
							prompt: "请选择",
							required: false,
							title: "任职方式",
							type: "selectbox",
							valueField: "planWorkWayCode",
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "probation",
							onSelect: "",
							prompt: "请输入",
							required: false,
							title: "试用期(天)",
							type: "numberbox",
							valueField: "probation",
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "workDate",
							onSelect: "",
							prompt: "请选择",
							required: false,
							title: "上岗日期",
							type: "datebox",
							valueField: "workDate",
						},
						{
							checkedit: null,
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "resignFlag",
							required: false,
							title: "辞去原岗位",
							type: "switchbox",
							valueField: "resignFlag"
						},
						{
							checkedit: null,
							data: [],
							editReadonly: null,
							isfull: null,
							max: "",
							min: "",
							nameField: "remark",
							prompt: "请输入",
							required: false,
							title: "行备注",
							type: "textarea",
							valueField: "remark"
						}
					]
				}]
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			clearUser(){
				let _that = this
				_that.$refs.autoform.clear("title0","userId")
				uni.removeStorage({
					key:_that.defaultDataCache
				})
			},
			formSubmit() {
				let _that = this
				_that.om.request(
					'tempInstAdjustMulti/getModelList',
					{
						reasonCode:_that.$refs.autoform.formdata.reasonCode,
						posId: _that.$refs.autoform.formdata.posId,
						planWorkWayCode: _that.$refs.autoform.formdata.planWorkWayCode,
						probation: _that.$refs.autoform.formdata.probation,
						resignFlag:_that.$refs.autoform.formdata.resignFlag,
						workDate: _that.$refs.autoform.formdata.workDate,
						remark: _that.$refs.autoform.formdata.remark,
						userIds: _that.$refs.autoform.formdata.userId
					},
					'POST'
				).then(res=>{
					try {
						uni.setStorageSync(_that.tempInstTemplateCache, res.value);
						uni.navigateBack()
					} catch (e) {
						// error
					}
				})
				
			},
			initLoad(options) {
				let _that = this
				let arr = [
					_that.om.request(
						"tempInstAdjustMulti/getReasonCodeList",
						{
							instId:_that.instId
						}
					),
					_that.om.request(
						"hrPosUtil/getTipAfterUser",
						{
							instId:_that.instId,
							formCode:'HRS0101',
							zposId:''
						}
					),
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'20387',
							inCode:'20388,20389'
						}
					)
				]
				Promise.all(arr).then(res=>{
					changeEditor('title0', 'userId', {
						'link': '../../public/searchUser/searchUser',
						linkParam: {
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							isUserPos:true,
							cachename: _that.userCacheKey,
							defaultDataCache: _that.defaultDataCache,
							isNew: true,
							defaultData: 'searchUserDefaultData',
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					}, null, _that.initConfigData);
					changeEditor("title0","reasonCode","data",configSelectData(res[0],"code","dicname"),_that.initConfigData)
					changeEditor("title0","posId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({}),
							namefield:"posname",
							initload:true,
							defaultData:JSON.stringify(res[1].value.attributes.pos),
							cachename:_that.posListCache
						}
					},null,_that.initConfigData)
					changeEditor("title0","planWorkWayCode","data",configSelectData(res[2],"code","dicname"),_that.initConfigData)
				})
			}
		},
		onLoad(options) {
			let _that = this
			uni.setNavigationBarTitle({
				title: '调岗模板'
			});
			if(options.instId){
				_that.instId = options.instId
			}
			_that.initLoad(options);
		},
		onUnload() {
			let _that = this
			uni.removeStorage({
				key: _that.defaultDataCache
			})
			//#ifdef APP-PLUS
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.userCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key: _that.defaultDataCache,
								data: d
							})
							var formdata = _that.$refs.autoform.formdata;
							if (d.length) {
								let idList = []
								let nameList = []
								let user3Id = ''
								for (let index in d) {
									idList.push(d[index].id + '/'+d[index].posId)
									nameList.push(d[index].username)
								}
								let idStr = idList.map(e => e).join(",")
								let nameStr = nameList.map(e => e).join(",");
								formdata.userId = idStr
								formdata.userName = nameStr
							}
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.posListCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.posId = d.posid
							formdata.posName = d.posname
							uni.removeStorage({
								key:_that.posListCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
